//
//  YWGroupBuyViewController.m
//  百布业务
//
//  Created by 陈炳桦 on 15/10/30.
//  Copyright © 2015年 陈炳桦. All rights reserved.
//  集中采购

#import "YWGroupBuyViewController.h"
#import "YWOtherDemandCell.h"
#import <MJRefresh.h>
#import <MJExtension.h>
#import "YWNetwork.h"
#import "YWMyDemand.h"
#import "YWDetailHomeViewController.h"

static NSString *ID = @"ID";

@interface YWGroupBuyViewController ()

// 模型数组
@property (nonatomic,strong) NSMutableArray *myDemands;

@end

@implementation YWGroupBuyViewController

- (NSMutableArray *)myDemands{
    if (_myDemands == nil) {
        _myDemands = [NSMutableArray array];
    }
    
    return _myDemands;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化设置
    [self setUp];
    
    // 设置刷新
    [self setupRefresh];
    
}

- (void)setUp{
    
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YWOtherDemandCell class]) bundle:nil] forCellReuseIdentifier:ID];
    
     self.view.backgroundColor = BBRGBColor(234, 236, 238);
    
    //去除下划线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
    
}

- (void)setupRefresh{
    // 下拉刷新
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewBuyerDemand)];
    [self.tableView.header beginRefreshing];
    
    
}

- (void)loadNewBuyerDemand{
    
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"pageNum"] = @0;
    parameters[@"numPerPage"] = @40;
    parameters[@"demandFrom"] = @9; // 集中采购是9
    
    [YWNetwork demandListWithParameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 字典数组 -> 模型数组
        NSMutableArray *array = [YWMyDemand objectArrayWithKeyValuesArray:responseObject[@"buyerDemandList"]];
        self.myDemands = array;
        
        
        [self.tableView reloadData];
        
        // 结束刷新
        [self.tableView.header endRefreshing];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self.tableView.header endRefreshing];
        
    }];
    
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.myDemands.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YWOtherDemandCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.mydemand = self.myDemands[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YWDetailHomeViewController *homeVC = [[YWDetailHomeViewController alloc] init];
    homeVC.mydemand = self.myDemands[indexPath.row];
    homeVC.flag = NO;
    [self.navigationController pushViewController:homeVC animated:YES];
}

@end
